/*==============================================================================
 * AUTH: Domenico Romano 5574331
 *       Matthew Baum, 9753095
 * 
 * DATE: January-29-2012
 * EDIT: March-03-2012
 * DESC: Java class to hold assignee at an array of Tasks.
 *==============================================================================*/

package SOEN_Phase_2;

// ArrayList for tasks assigned
import java.util.ArrayList;

public class Person
{

    // Employee ID
    private int ID = 0;

    // Assignee Name
    private String name;

    // List of tasks assigned
    private ArrayList<Task> tasks;

    // Sum of hours assigned
    private int hours = 0;

    // Constructor
    public Person(String name, int id)
    {
        // Set employee info
    	this.name = name;
        ID = id;

        // Init tasks list
        tasks = new ArrayList<Task>();
    }

    /*==================================================
     * Format Person as a GUI Table Entry (string[])
     *
     * Fields:
     * - ID
     * - Name
     * - Tasks
     * - Hours
     *==================================================*/
    public String[] toGUIEntry()
    {
        // Array to hold formatted result
        String[] guiEntry = new String[3];

        // Populate ID field
        guiEntry[0] = Integer.toString(ID);

        // Populate Name field
        //guiEntry[1] = name;

        // Populate Tasks field
        guiEntry[1] = tasksToString();

        // Populate hours field
        guiEntry[2] = Integer.toString(hours);

        // Return the formatted result
        return guiEntry;
    }

    // Private helper to stringify tasks
    // WARNING: this function is NOT efficient
    private String tasksToString()
    {
        // Tasks string
        String taskString = "";

        // Build the string from the assigned tasks
        for(int i = 0; i != tasks.size(); ++i)
        {
            taskString += tasks.get(i).getID();

            if(i < tasks.size() - 1)
            {
                taskString += ", ";
            }
        }

        // Return the string
        return taskString;
    }

    // Add a task to this person
    public void addTask(Task task)
    {
        tasks.add(task);

        // Recalculate hours
        calculateHours();
    }

    // Calculate the sum of hours assigned to this Person
    public void calculateHours()
    {
        hours = 0;

        for(int i = 0; i != tasks.size(); ++i)
        {
            hours += tasks.get(i).getDuration();
        }
    }

    // Print to String
    @Override
    public String toString()
    {
        return "IndvTasks [assgn=" + name + ", tasks="
                + tasks + ", getClass()=" + getClass()
                + ", hashCode()=" + hashCode() + ", toString()="
                + super.toString() + "]";
    }

        // Hash Code method: Required to use this class as a key in a hash table
    @Override
    public int hashCode()
    {
        // Return the Employee ID, which is assumed to be unique
        return ID;
    }

    // Equals method: Required to use this class as a key in a hash table
    @Override
    public boolean equals(Object other)
    {
        // Tasks are considered equal if they have the same ID
        if(this != other && other instanceof Person)
        {
            Person otherPerson = (Person)other;

            if(this.ID == otherPerson.ID)
            {
                return true;
            }
        }

        // They are not considered equal!
        return false;
    }

    // *** GETTERS AND SETTERS ***//

    // Return assignee name
    public String getName() {
        return name;
    }

    // Get ID
    public int getID()
    {
        return ID;
    }

    // Return array of assigned Tasks
    public ArrayList<Task> getTasks() {
        return tasks;
    }

    // Return total hours assigned
    public int getHours()
    {
        return hours;
    }

    // Set assignee name
    public void setName(String name) {
       this.name = name;
    }

    // Set ID
    public void setID(int id)
    {
        ID = id;
    }
}
